<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/12/05
 * Time: 15:51
 */

namespace HiCommon\Model;

class OrderCheckpointRefund extends Base
{
    
    protected $table = 'order_checkpoint_refund';
    
    protected $guarded = [];
    
    //public $timestamps = false;
    
    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];


    /**
     * 关联年检预约订单表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order_checkpoint(){
        return $this->belongsTo(OrderCheckpoint::class,'order_sn','order_sn')->select(['id','order_sn','main_type','payment_method','price']);
    }

    /**
     * 关联年检预约表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_for_user()
    {
        return $this->belongsTo(OrderCheckpoint::class, 'order_sn', 'order_sn')->with(['shop','user_company'])->select(['id','order_sn','main_type','shop_id','user_company_id','phone','realname','appointment_date','appointment_times','appointment_time_type']);
    }

    /**
     * 关联年检预约表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_for_shop()
    {
        return $this->belongsTo(OrderCheckpoint::class, 'order_sn', 'order_sn')->with(['user','user_company'])->select(['id','order_sn','main_type','shop_id','user_id','user_company_id','phone','realname','appointment_date','appointment_times','appointment_time_type','created_at','pay_time']);
    }

    /**
     * 关联年检预约订单车辆表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function vehicle(){
        return $this->belongsTo(OrderCheckpointVehicle::class,'order_checkpoint_vehicle_id')->with(['user_vehicle'])->select(['id','order_sn','users_vehicle_id','users_vehicle_id','plate_no','is_call_helpdriving','owner','price','apply_refund_state','check_state']);
    }

    /**
     * 关联年检预约订单车辆表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function vehicle_detail(){
        return $this->belongsTo(OrderCheckpointVehicle::class,'order_checkpoint_vehicle_id')->with(['user_vehicle']);
    }

    /**
     * 关联年检预约订单车辆表-故障原因
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function fail_type(){
        return $this->belongsTo(OrderCheckpointVehicle::class,'order_checkpoint_vehicle_id')->select(['id','fail_type']);
    }

    /**
     * 关联年检预约订单车辆表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order_checkpoint_vehicle(){
        return $this->belongsTo(OrderCheckpointVehicle::class,'order_checkpoint_vehicle_id')->select(['id','order_sn','plate_no','extend']);
    }

    public function njyy_vehicle(){
        return $this->belongsTo(OrderCheckpointVehicle::class,'order_checkpoint_vehicle_id');

    }


    /**
     * 关联退款表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_pay_refund(){

        return $this->hasOne(OrderPayRefund::class,'order_sn','refund_sn');
    }


    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
    
        /**
         * 获取图片可用链接
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getImgUrlAttribute($value)
        {
            $arr_data = [
              'src'=>$value,
              'full_src'=>oss_full_url($value),
            ];
            return $arr_data;
        }
        
        /**
         * 扩展信息
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getExtendAttribute()
        {
            $n_type = $this->type;
            switch ($n_type)
            {
                //售后类型 1-用户申请退款 2-不符合检测条件
                case 1:
                    $arr_data['type'] = '用户申请退款';
                    break;
                case 2:
                    $arr_data['type'] = '不符合检测条件';
                    break;
                default:
                    $arr_data['type'] = '';
            }

            $n_apply_refund_state = $this->apply_refund_state;
            switch ($n_apply_refund_state)
            {
                //申请退款状态：0无 1申请中 2申请成功-待退款 （通过）3申请成功-已退款 （通过）9申请失败（拒绝）
                case 1:
                    $arr_data['apply_refund_state'] = '申请中';
                    break;
                case 2:
                    $arr_data['apply_refund_state'] = '申请成功-待退款';
                    break;
                case 3:
                    $arr_data['apply_refund_state'] = '申请成功-已退款';
                    break;
                case 9:
                    $arr_data['apply_refund_state'] = '申请失败';
                    break;
                default:
                    $arr_data['apply_refund_state'] = '';
            }


//            $arr_data               = $this->arr_state_display_name();
//            $arr_data['created_at'] = ymd_friendly_date($this->created_at);
//            $arr_data['api'] = $this->table_field_api();
            return $arr_data;
        }

    /**
     * state 状态值数组
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function state_arr()
    {
        //申请类型： 用户申请退款 不可检测商家单辆退款 13不可检测商家全部退款
        $arr_type = [
          ['id' => -99, 'name' => '申请类型'],
          ['id' => 1, 'name' => '用户申请退款'],
          ['id' => 2, 'name' => '不可检测商家单辆退款'],
          ['id' => 13, 'name' => '不可检测商家全部退款'],
        ];

        //申请退款状态：0-无 1-申请中 2-待退款 3-退款成功 9-申请失败
        $arr_apply_refund_state = [
          ['id' => -99, 'name' => '申请退款状态'],
          ['id' => 0, 'name' => '无'],
          ['id' => 1, 'name' => '申请中'],
          ['id' => 2, 'name' => '待退款'],
          ['id' => 3, 'name' => '退款成功'],
          ['id' => 9, 'name' => '申请失败'],
        ];

        $arr_data = [
          'apply_refund_state' => $arr_apply_refund_state,
          'type'        => $arr_type,
        ];
        return $arr_data;
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_name()
    {
        return $this->hi_arr_state_name($this->state_arr());
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_display_name()
    {
        return $this->hi_arr_state_display_name($this->state_arr());
    }

    
  
}